﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类tbCompain。
    /// </summary>
    public class DALCompain
    {
        public DALCompain()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CompainId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbCompain");
            strSql.Append(" where CompainId=@CompainId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CompainId", SqlDbType.Int,4)};
            parameters[0].Value = CompainId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbCompain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbCompain(");
            strSql.Append("CompainName)");
            strSql.Append(" values (");
            strSql.Append("@CompainName)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@CompainName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.CompainName;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Gift.Model.tbCompain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbCompain set ");
            strSql.Append("CompainName=@CompainName");
            strSql.Append(" where CompainId=@CompainId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CompainId", SqlDbType.Int,4),
					new SqlParameter("@CompainName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.CompainId;
            parameters[1].Value = model.CompainName;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int CompainId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbCompain ");
            strSql.Append(" where CompainId=@CompainId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CompainId", SqlDbType.Int,4)};
            parameters[0].Value = CompainId;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbCompain GetModel(int CompainId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CompainId,CompainName from tbCompain ");
            strSql.Append(" where CompainId=@CompainId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CompainId", SqlDbType.Int,4)};
            parameters[0].Value = CompainId;

            Gift.Model.tbCompain model = new Gift.Model.tbCompain();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CompainId"].ToString() != "")
                {
                    model.CompainId = int.Parse(ds.Tables[0].Rows[0]["CompainId"].ToString());
                }
                model.CompainName = ds.Tables[0].Rows[0]["CompainName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CompainId,CompainName ");
            strSql.Append(" FROM tbCompain ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbCompain";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

